package com.smile.web.home;

import com.smile.config.entity.User;
import com.smile.config.entity.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @RequestMapping("")
    public ModelAndView homePage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/views/home");

        modelAndView.addObject("users", homeService.findAllUser());

        return modelAndView;
    }

    @GetMapping("/addUser")
    @ResponseBody
    public void addUser() {
        homeService.addUser();
    }

    @GetMapping("/deleteUser")
    @ResponseBody
    public void deleteUser() {
        homeService.deleteUser(homeService.findAllUser().get(0));
    }

    @GetMapping("/updateUser")
    @ResponseBody
    public void updateUser() {
        homeService.updateUser(homeService.findAllUser().get(0));
    }

    @GetMapping("/showUser")
    @ResponseBody
    public User showUser() {
        return homeService.findUserById(4L);
    }

    @GetMapping("/showUsers")
    @ResponseBody
    public List<User> showUsers() {
        return homeService.findAllUser();
    }

}
